package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleController {
    @Reference
    private RoleService roleService;
    @RequestMapping("/pageQuery")
    public PageResult pageQuery(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult=roleService.pageQuery(
                queryPageBean.getCurrentPage(),
                queryPageBean.getPageSize(),
                queryPageBean.getQueryString()
        );
        return pageResult;
    }
    @RequestMapping("/findById")
    public Result findById(Integer id){
        Role role=roleService.findById(id);
        if(role!=null){
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS,role);
        }
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
    }
    @RequestMapping("/findMenuIdsByRoleId")
    public Result findMenuIdsByRoleId(Integer id){
        try {
            List<Integer> list=roleService.findMenuIdsByRoleId(id);
            return new Result(true, MessageConstant.QUERY_MENU_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_MENU_FAIL);
        }

    }
    @RequestMapping("/findPermissionIdsByRoleId")
    public Result findPermissionIdsByRoleId(Integer id){
        try {
            List<Integer> list=roleService.findPermissionIdsByRoleId(id);
            return new Result(true, MessageConstant.QUERY_PERMISSION_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_PERMISSION_FAIL);
        }
    }
    @RequestMapping("/edit")
    public Result edit(@RequestBody Role role, Integer[] menuIds, Integer[] permissionIds){
        try {
            roleService.edit(role,menuIds,permissionIds);
            return new Result(true, MessageConstant.EDIT_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_ROLE_FAIL);
        }
    }
    @RequestMapping("/add")
    public Result add(@RequestBody Role role, Integer[] menuIds, Integer[] permissionIds){
        try {
            roleService.add(role,menuIds,permissionIds);
            return new Result(true, MessageConstant.ADD_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_ROLE_FAIL);
        }
    }
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            roleService.delete(id);
            return new Result(true, MessageConstant.DELETE_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_ROLE_FAIL);
        }
    }
        /**
        赵世杰
         */
    //查询所有
    @RequestMapping("/findAll")
    public Result findAll(){
        List<Role> roleList = roleService.findAll();
        if(roleList != null && roleList.size() > 0){
            Result result = new Result(true, MessageConstant.QUERY_USER_SUCCESS);
            result.setData(roleList);
            return result;
        }
        return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
    }
}
